package com.apes.fn.scm.lossSale;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.crm.business.model.InsuranceCompany;
import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author wuq
 * @Date 2021-12-2
 * @Description 定损流程调整销售
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = LossSaleAdjust.CONST_TABLE_NAME)
public class LossSaleAdjust extends Model {
    public final static String CONST_TABLE_NAME = "Loss_Sale_Adjust";

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "定损流程单据调整", padding = 5, prefix = "DT%(company)%(yy)%(month)", useDateRange = true)
    @Comment("调整单号")
    private String id;

    @Comment("定损单号")
    @Column(name = "loss_Quotation_Id", length = 30)
    private String lossQuotationId;

    @Comment("零售单号")
    @Column(name = "retail_id", length = 30)
    private String retailId;

    @Comment("定损门店公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    private PartyRole company;

    @Comment("定损门店")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    private Dept dept;

    @Comment("接车门店")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "get_car_dept_id", referencedColumnName = "id")
    private Dept getCarDept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "old_insurance_company_id", referencedColumnName = "id")
    @Comment("保险公司")
    private InsuranceCompany oldInsuranceCompany; //原保险公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "new_insurance_company_id", referencedColumnName = "id")
    @Comment("保险公司")
    private InsuranceCompany newInsuranceCompany; //新保险公司

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("调整金额")
    private double amount = 0; //调整金额

    @Column(name = "adjust_amount", columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("调整幅度金额")
    private double adjustAmount = 0; //调整金额

    @Column(name = "original_amount", columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("原定损金额")
    private double originalAmount = 0; //调整金额

    @Comment("会员扩展信息")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_expand_id", referencedColumnName = "id")
    private FnCustomerExpand customerExpand;

    @Comment("订单备注")
    @Column(name = "remark", length = 120)
    private String remark;

    @Comment("退回备注")
    @Column(name = "go_back_remark", length = 120)
    private String goBackRemark;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "loss_Adjust_Reason", referencedColumnName = "id")
    @Comment("调整原因")
    private Enumeration lossAdjustReason;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUID; //审核

    @Comment("审批日期")
    @Column(name = "date_approve")
    private Date dateApprove;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_id", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteId; //删除人

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate;    // 删除日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "confirm_id", referencedColumnName = "id")
    @Comment("确认人")
    private PartyRole confirmId; // 确认人

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "confirm_date")
    @Comment("确认日期")
    private Date confirmDate;    // 确认日期

    @Comment("sent:创建;confirm:确认;approve:审核;delete:删除")
    @Column(name = "state", length = 10, nullable = false)
    private String state = "sent";  // sent:创建;approve:审核;delete:删除

    /* {orderType:"", orderId:"", "company":"", "partner":"", "voucherType":"", "amount":""} */
    @Transient
    private List<LossSaleAdjustAccounts> referenceAccounts;

    public List<LossSaleAdjustAccounts> getReferenceAccounts(){
        if (StringUtils.isEmpty(this.retailId)) return new ArrayList<>();
        return SpringManager.getBean(LossSaleAdjustReturnService.class).findReferenceAccounts(this.retailId);
    }

}
